package com.study.hutool.excel;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author zhuzi
 * 2023/10/25 19:55
 */
public class Read {  public static final String BasePath = "/Users/zhuzi/Desktop/pics/";
    @Test
    public void test(){
        ExcelReader reader = ExcelUtil.getReader(BasePath + "writeTest.xlsx");

        reader.setSheet(1);
        List<List<Object>> read = reader.read();
        print(read);

        reader.setSheet(0);
        List<List<Object>> read1 = reader.read();
        print(read1);

    }
    public void print(List<List<Object>> list){
        for (List<Object> objects : list) {
            System.out.println(objects);
        }
        System.out.println();
    }

    @Test
    public void readExcel(){
        String path="/Users/zhuzi/workspace/a-wk/J-乌兰察布/B-其他功能设计/市场体系建设科/附件1、2、3（农畜产品市场等经营场所情况信息表）.xls";
        ExcelReader reader = ExcelUtil.getReader(path);

        List<Sheet> sheets = reader.getSheets();

        for (Sheet sheet : sheets) {
            reader.setSheet(sheet);
            List<List<Object>> read = reader.read();
            print(read);
            List<Map<String, Object>> maps = reader.readAll();
            for (Map<String, Object> map : maps) {
                //System.out.println(map);
            }
        }
    }

    //读取指定坐标i
    @Test
    public void readExcelCellValue(){
        String path="/Users/zhuzi/workspace/a-wk/J-乌兰察布/B-其他功能设计/市场体系建设科/附件1、2、3（农畜产品市场等经营场所情况信息表）.xls";
        ExcelReader reader = ExcelUtil.getReader(path);

        reader.setSheet(2);
        List<List<Object>> read = reader.read();
        print(read);

        Object x = reader.readCellValue(5, 1);
        Cell cell = reader.getCell(5, 1);
        System.out.println(cell);
        System.out.println(x);
        for (int i = 0; i <=6; i++) {
            for (int j = 0; j < 3; j++) {
                Object cellValue = reader.readCellValue(i, j);
                System.out.println(cellValue);
            }
            System.out.println();
        }


    }
}
